package com.biovr.backend.service;

import com.biovr.backend.domain.Apparatus;
import com.biovr.backend.repository.ApparatusRepository;
import com.biovr.backend.utils.DateTimeUtils;
import com.biovr.backend.utils.QrCodeUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by monodev on 2017/5/10.
 */
@Service
public class ApparatusServiceImpl implements ApparatusService {
    @Autowired
    DateTimeUtils dateTimeUtils;
    @Autowired
    ApparatusRepository apparatusRepository;
    @Autowired
    QrCodeUtils qrCodeUtils;
   @Value("${tbname.apparatus}")
   private  String tbName;
    /**
     * 添加新仪器
     * @param request
     * @param apparatus
     * @return
     */
    @Override
    public Apparatus addApparatus(HttpServletRequest request, Apparatus apparatus) {

        apparatus.setTableName(tbName);
        apparatus.setStatus(true);
        apparatus.setCreateTime(dateTimeUtils.getCurrDateTimeString());
        apparatus = apparatusRepository.save(apparatus);
        String qrcode = generateQrcode(request,apparatus);
        apparatus.setQCode(qrcode);
        apparatus= apparatusRepository.save(apparatus);
        return apparatus;
    }

    /**
     * 重新生成二维码
     * @param request
     * @param apparatusid
     * @return
     */
    public String regenerateQrcode(HttpServletRequest request, String apparatusid) throws Exception{
        Apparatus apparatus = apparatusRepository.findOne(apparatusid);
        if(null!=apparatus){
            qrCodeUtils.deleteQrcode(apparatus.getQCode());
            String qrcode = generateQrcode(request,apparatus);
            apparatus.setQCode(qrcode);
            apparatusRepository.save(apparatus);
            return apparatus.getQCode();
        }
        return null;
    }
    /**
     * 生成二维码
     * @param request
     * @param apparatus
     * @return
     */
    private String generateQrcode(HttpServletRequest request, Apparatus apparatus){
        //生成二维码
        //获得根请求地址
       // int port= -1;
       // port=request.getLocalPort();
        //String basePath = request.getScheme()+"://"+request.getServerName()+(port==-1?"/":":"+port+"/");
        String basePath = request.getScheme()+"://"+request.getServerName()+"/";
        //根据rest规则生成链接
        String qCodeContent = basePath+"apparatuses/"+apparatus.getId();
        try {
            //这是base64编码二维码，因生成的二维码数据量太大，已弃用
            // newExpm.setQCode(qrCodeUtils.getBase64QRCode(qCodeContent));
            String qCodeFileName = UUID.randomUUID().toString().replace("-","")+".jpg";
            qrCodeUtils.saveQRCodeToFile(qCodeContent,qCodeFileName,null);
            //apparatus.setQCode(qCodeFileName);
            return qCodeFileName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据ID删除
     * @param id
     */
    public  boolean deleteApparatus(String id) throws Exception{
        if (apparatusRepository.exists(id))
        {
            Apparatus e = apparatusRepository.findOne(id);
            qrCodeUtils.deleteQrcode(e.getQCode());
            apparatusRepository.delete(e);
            return true;
        }
        return  false;
    }

}
